﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GoogleAPIOnWindowsPhone.Resources;
using GoogleAPIOnWindowsPhone.ViewModel;
using GoogleAPIOnWindowsPhone.Model;
using Microsoft.Phone.Maps.Toolkit;
using Microsoft.Phone.Maps.Controls;
using System.Device.Location;
using System.Windows.Media;
using System.IO.IsolatedStorage;
using Windows.Devices.Geolocation;

namespace GoogleAPIOnWindowsPhone
{

    public partial class MainPage : PhoneApplicationPage
    {
        MainViewModel vm = new MainViewModel();
        MainViewModel vm2 = new MainViewModel();
       

        
         
     
        // Constructor
        public MainPage()
        {
            InitializeComponent();


             
            
           //this.DataContext = this.vm;
            //foreach (var item in vm.PlacesList)
            //{
            //    Pushpin pin = new Pushpin();
            //    pin.GeoCoordinate = new GeoCoordinate(item.Latitude,item.Longitude);
            //    //pin.pushpin_data = item;
            //    pin.Content = item.ShortName;
            //    pin.Foreground = new SolidColorBrush(Colors.White);
                
                
            //    pin.DoubleTap += pin_DoubleTap;

            //    MapOverlay overlayer = new MapOverlay();
            //    overlayer.GeoCoordinate = new GeoCoordinate(item.Latitude, item.Longitude);
              
            //    overlayer.Content = pin;
            //    layer.Add(overlayer);
            //}

            //mymap.Layers.Add(layer);
        
            //OptionSelection.ItemsSource = vm.list;

          
           
           datashowonLonglist.ItemsSource = vm.PlacesList;
           datashowonLonglist2.ItemsSource = vm.Area;
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        void pin_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Pushpin pushpin = sender as Pushpin;
            ATMBoothList atm = new ATMBoothList();
            string foo = pushpin.GeoCoordinate.ToString();
            string bar = "";
            foreach (var item in vm.PlacesList)
            {
                bar = item.Latitude.ToString() + ", " + item.Longitude.ToString();
                if (foo == bar)
                {
                    atm = item;
                }
            }
            MessageBox.Show(pushpin.Content.ToString()+"\n"+ atm.Vicinity);
            
        }

       

        //private void OptionSelection_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{

        //    vm.PlacesList.Select(o => o).Where(o => o.Types == "bank").ToList();
        //    vm2.PlacesList.Clear();
        //    //var sen = (sender as ListPicker).SelectedItem as ListPickerItem;




        //    var val = (TypeOFList)(sender as ListPicker).SelectedItem;
        //    string foo = val.Types;
        //    foreach (var item in vm.PlacesList)
        //    {
        //        if (item.Types == foo)
        //        {
        //            vm2.PlacesList.Add(item);
        //        }
        //    }

        //    //datashowonLonglist.ItemsSource = vm2.PlacesList;
            
        //    ContentPanel.Visibility = Visibility.Collapsed;
         

        //}
        private void PitchValueChanged(object sender, EventArgs e)
        {
            if (PitchSlider == null)
                return;

            //mymap.Pitch = PitchSlider.Value;
            mymap.Heading = PitchSlider.Value;
        }

        private void zoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (zoom == null)
            {
                return;
            }
            mymap.ZoomLevel = zoom.Value;
        }

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((e.Orientation & PageOrientation.Portrait) == (PageOrientation.Portrait))
            {
                mymap.Height = 677;
                mymap.Width = 474;
               
            }
            else
            {
                mymap.Height = 474;
                mymap.Width = 677;
            }
        }

        

        private void datashowonLonglist_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            var val = (ATMBoothList)(sender as LongListSelector).SelectedItem;



            string foo = val.Name;
            
            List<ATMBoothList> bankWiseList = new List<ATMBoothList>();
             
            foreach (var item in vm.PlacesList)
            {
                if (foo == "All ATM Booths IN Dhaka")
                {
                    bankWiseList.Add(item);
                }
                else if (foo == item.Name)
                {
                    bankWiseList.Add(item);
                }
            }

           mymap.Layers.Clear();
            
         
            MapLayer layer1 = new MapLayer();
            foreach (var item in bankWiseList)
            {
                Pushpin pin = new Pushpin();
                pin.GeoCoordinate = new GeoCoordinate(item.Latitude, item.Longitude);
                //pin.pushpin_data = item;
                pin.Content = item.ShortName;
                pin.Foreground = new SolidColorBrush(Colors.White);


                pin.DoubleTap += pin_DoubleTap;

                MapOverlay overlayer = new MapOverlay();
                overlayer.GeoCoordinate = new GeoCoordinate(item.Latitude, item.Longitude);

                overlayer.Content = pin;
                layer1.Add(overlayer);
            }
            if (layer1 != null)
            {
                mymap.Layers.Add(layer1);
            }
            else
            {
                return;
            }

            mymap.SetView(new GeoCoordinate(23.759267, 90.388931), 12);
            ContentPanel.Visibility = Visibility.Collapsed;
        }
        private void datashowonLonglist2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            var val = (LocalityClass)(sender as LongListSelector).SelectedItem;



            string foo = val.Locality;

            List<LocalityClass> areaWiseList = new List<LocalityClass>();
            foreach (var item in vm.Area)
            {
                if (foo == "Dhaka")
                {
                    areaWiseList.Add(item);
                }
                else if (foo == item.Locality)
                {
                    areaWiseList.Add(item);
                }
            }

            //mymap.Layers.Clear();


            //MapLayer layer1 = new MapLayer();
            foreach (var item in areaWiseList)
            {
                //Pushpin pinArea = new Pushpin();
                //pinArea.GeoCoordinate = new GeoCoordinate(item.Latitude, item.Longitude);
                ////pin.pushpin_data = item;
                //pinArea.Content = item.Locality;
                //pinArea.Foreground = new SolidColorBrush(Colors.White);


                //pinArea.DoubleTap += pinArea_DoubleTap;

                //MapOverlay overlayer = new MapOverlay();
                //overlayer.GeoCoordinate = new GeoCoordinate(item.Latitude, item.Longitude);

                //overlayer.Content = pinArea;
                //layer1.Add(overlayer);
                mymap.SetView(new GeoCoordinate(item.Latitude, item.Longitude), 16);
            }
            //if (layer1 != null)
            //{
            //    mymap.Layers.Add(layer1);
            //}
            //else
            //{
            //    return;
            //}


            ContentPanel2.Visibility = Visibility.Collapsed;
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
            {
                // User has opted in or out of Location
                return;
            }
            else
            {
                MessageBoxResult result =
                    MessageBox.Show("This app accesses your phone's location. Is that ok?","Location", MessageBoxButton.OKCancel);

                if (result == MessageBoxResult.OK)
                {
                    IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
                }
                else
                {
                    IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
                }

                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }
        private async void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            //mymap.Layers.Clear();
            MapLayer layer = new MapLayer();
             
            if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] != true)
            {
                // The user has opted out of Location.
                return;
            }

            Geolocator geolocator = new Geolocator();
            geolocator.DesiredAccuracyInMeters = 50;

            try
            {
                Pushpin myPosition = new Pushpin();
                Geoposition geoposition = await geolocator.GetGeopositionAsync(
                    maximumAge: TimeSpan.FromMinutes(5),
                    timeout: TimeSpan.FromSeconds(10)
                    );
 
                //myPosition.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
        
                myPosition.Content = "Your Current Position";
                //myPosition.Foreground = new SolidColorBrush(Colors.White);


                myPosition.DoubleTap += myPosition_DoubleTap;

                MapOverlay overlayer = new MapOverlay();
                //overlayer.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
                overlayer.GeoCoordinate = new GeoCoordinate(23.794769, 90.414463);
                mymap.SetView(new GeoCoordinate(23.794769, 90.414463),16);
                overlayer.Content = myPosition;
                layer.Add(overlayer);
                mymap.Focus();
                
                mymap.Layers.Add(layer);
            }
            catch (Exception ex)
            {
                if ((uint)ex.HResult == 0x80004004)
                {
                    // the application does not have the right capability or the location master switch is off
                    MessageBox.Show("location  is disabled in phone settings.");
                }
                //else
                {
                    // something else happened acquring the location
                }
            }
        }

        void myPosition_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            MessageBox.Show("Your Current Location");
        }
        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            ContentPanel2.Visibility = Visibility.Collapsed;
            ContentPanel.Visibility = Visibility.Visible;
        }
        private void ApplicationBarMenuItem_Click_1(object sender, EventArgs e)
        {
            ContentPanel.Visibility = Visibility.Collapsed;
            ContentPanel2.Visibility = Visibility.Visible;
        }

        

        //void pinArea_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
        //{
        //    Pushpin pushpin = sender as Pushpin;
        //    LocalityClass atm = new LocalityClass();
        //    string foo = pushpin.GeoCoordinate.ToString();
        //    string bar = "";
        //    foreach (var item in vm.Area)
        //    {
        //        bar = item.Latitude.ToString() + ", " + item.Longitude.ToString();
        //        if (foo == bar)
        //        {
        //            atm = item;
        //        }
        //    }
        //    MessageBox.Show(pushpin.Content.ToString() + "\n" + atm.Locality);
        //}

        

        
     

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}

         
    }
}